package com.sky.trade.controller;

import com.sky.api.domain.dto.GoodsSalesDTO;
import com.sky.common.result.PageResult;
import com.sky.common.result.Result;
import com.sky.trade.domain.dto.OrdersCancelDTO;
import com.sky.trade.domain.dto.OrdersConfirmDTO;
import com.sky.trade.domain.dto.OrdersPageQueryDTO;
import com.sky.trade.domain.dto.OrdersRejectionDTO;
import com.sky.trade.domain.vo.OrderStatisticsVO;
import com.sky.trade.domain.vo.OrderVO;
import com.sky.trade.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单管理
 *
 * @author toki
 */
@RestController("adminOrderController")
@RequestMapping("/admin/order")
@Slf4j
@Api(tags = "订单管理接口")
public class AdminOrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 订单搜索
     *
     * @param ordersPageQueryDTO 订单搜索条件
     * @return 订单分页信息
     */
    @GetMapping("/conditionSearch")
    @ApiOperation("订单搜索")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("订单搜索 ordersPageQueryDTO={}", ordersPageQueryDTO);
        PageResult pageResult = orderService.conditionSearch(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 各个状态的订单数量统计
     *
     * @return 各个状态的订单数量统计
     */
    @GetMapping("/statistics")
    @ApiOperation("各个状态的订单数量统计")
    public Result<OrderStatisticsVO> statistics() {
        log.info("各个状态的订单数量统计");
        return Result.success(orderService.statistics());
    }

    @GetMapping("/details/{id}")
    @ApiOperation("订单详情")
    public Result<OrderVO> details(@PathVariable("id") Long id) {
        log.info("订单详情 id={}", id);
        return Result.success(orderService.details(id));
    }

    /**
     * 确认订单
     *
     * @param ordersConfirmDTO 确认订单信息
     * @return 成功或失败
     */
    @PutMapping("/confirm")
    @ApiOperation("确认订单")
    public Result confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        log.info("确认订单 ordersConfirmDTO={}", ordersConfirmDTO);
        orderService.confirm(ordersConfirmDTO);
        return Result.success();
    }

    /**
     * 拒绝订单
     *
     * @param ordersRejectionDTO 拒绝订单信息
     * @return 成功或失败
     */
    @PutMapping("/rejection")
    @ApiOperation("订单拒绝")
    public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) throws Exception {
        log.info("订单拒绝 ordersRejectionDTO={}", ordersRejectionDTO);
        orderService.rejection(ordersRejectionDTO);
        return Result.success();
    }

    /**
     * 订单取消
     *
     * @param ordersCancelDTO 订单取消信息
     */
    @PutMapping("/cancel")
    @ApiOperation("订单取消")
    public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) throws Exception {
        log.info("订单取消 ordersCancelDTO={}", ordersCancelDTO);
        orderService.cancel(ordersCancelDTO);
        return Result.success();
    }

    /**
     * 订单派送
     *
     * @param id 订单id
     */
    @PutMapping("/delivery/{id}")
    @ApiOperation("订单派送")
    public Result delivery(@PathVariable("id") Long id) {
        log.info("订单派送 id={}", id);
        orderService.delivery(id);
        return Result.success();
    }

    /**
     * 完成订单
     *
     * @return 成功或失败
     */
    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单")
    public Result complete(@PathVariable("id") Long id) {
        orderService.complete(id);
        return Result.success();
    }

    /**
     * 销量排名统计
     *
     * @param begin 开始日期
     * @param end   结束日期
     * @return 销量排名统计
     */
    @GetMapping("/top10")
    public Result<List<GoodsSalesDTO>> getSalesTop10(@RequestParam(name = "begin", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime begin,
                                                     @RequestParam(name = "end", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end) {
        return Result.success(orderService.getSalesTop10(begin, end));
    }

    @GetMapping("/turnoverStatistics")
    @ApiOperation("营业额数据统计")
    public Result<Double> sumByMap(@RequestParam(name = "begin", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime begin,
                                   @RequestParam(name = "end", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end,
                                   @RequestParam(name = "status", required = false) Integer status) {
        Map<String, Object> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);
        map.put("status", status);
        return Result.success(orderService.sumByMap(map));
    }

    @GetMapping("/ordersStatistics")
    @ApiOperation("用户数据统计")
    public Result<Integer> countByMap(@RequestParam(name = "begin", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime begin,
                                      @RequestParam(name = "end", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end,
                                      @RequestParam(name = "status", required = false) Integer status) {
        Map<String, Object> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);
        map.put("status", status);
        return Result.success(orderService.countByMap(map));
    }

}
